package com.smf.lsc.security.converter;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 自定义jwt认证信息转换
 * Created on 2021/11/19.
 *
 * @author ln
 * @since 1.0
 */
public class MyUserAuthenticationConverter implements UserAuthenticationConverter {
//	private static final String USER_ID = "user_id";
//	private static final String DEPT_ID = "dept_id";
//	private static final String TENANT_ID = "tenant_id";
//	private static final String N_A = "N/A";
	
	private final String USER_INFO_KEY = "userinfo";
	
	@Override
	public Authentication extractAuthentication(Map<String, ?> map) {
		if (map.containsKey(USERNAME)) {
			Object principal = map.get(USERNAME);
			Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
//			if (userDetailsService != null) {
//				UserDetails user = userDetailsService.loadUserByUsername((String) map.get(USERNAME));
//				authorities = user.getAuthorities();
//				principal = user;
//			}
			if(map.get(USER_INFO_KEY)!=null && !"".equals(map.get(USER_INFO_KEY).toString()))
			{
				Map<String, Serializable> extensions = new HashMap<String, Serializable>();
				JSONObject userJson = JSONObject.parseObject(map.get(USER_INFO_KEY).toString());
				MyUserDetails myUser = JSON.toJavaObject(userJson,MyUserDetails.class);
				principal=myUser;
			}
			return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
		}
		return null;
	}

	private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
		Object authorities = map.get(AUTHORITIES);
		if (authorities instanceof String) {
			return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
		}
		if (authorities instanceof Collection) {
			return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
					.collectionToCommaDelimitedString((Collection<?>) authorities));
		}
		throw new IllegalArgumentException("Authorities must be either a String or a Collection");
	}

	@Override
	public Map<String, ?> convertUserAuthentication(Authentication authentication) {
		Map<String, Object> response = new LinkedHashMap<String, Object>();
		response.put(USERNAME, authentication.getName());
		if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
			response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
		}
		return response;
	}
}